package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//引入生产者 =消费者模型
public class Demo30 {
       public static void main(String[] args) {
           //阻塞队列
          // BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
           BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();


           //消费者
           Thread t1 = new Thread(() -> {
               try{
               while (true) {
                   Integer value = queue.take();
                   System.out.println("t1消费：" + value);
                   Thread.sleep(1000);
               }
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           });

           //生产者
           Thread t2 = new Thread(() -> {
               try {
                   int count = 1;
                   while (true) {
                       queue.put(count);
                       System.out.println("t2生产：" + count);
                       count++;
                       //Thread.sleep(1000);
                   }
               }catch (InterruptedException e) {
                   e.printStackTrace();
               }
           });
           t1.start();
           t2.start();
       }
}
